package world.xuewei.controller;


import world.xuewei.model.entity.Address;
import world.xuewei.model.entity.R;
import world.xuewei.service.AddressService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 收货地址表(Address)表控制层
 *
 * @author makejava
 * @since 2024-11-15 19:27:47
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    /**
     * 服务对象
     */
    @Resource
    private AddressService addressService;


    /**
     * 查询当前用户的地址
     * @param request 用户ID
     * @return 查询结果
     */
    @GetMapping
    public R queryByPage(HttpServletRequest request) {
        // 1. 获取当前登录用户ID
        String userIdStr = (String) request.getAttribute("userId");
        if (userIdStr == null) {
            return R.error().message("用户未登录 or 携带token请求");
        }
        Long userId = Long.parseLong(userIdStr);
        List<Address> addressList = addressService.queryByUserAddress(userId);
        return R.ok().data("addressList", addressList);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok().data(this.addressService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param address 实体
     * @return 新增结果
     */
    @PostMapping
    public R add(@RequestBody Address address) {
        String phoneRegex = "^1[3-9]\\d{9}$";
        if (!address.getReceiverPhone().matches(phoneRegex)) {
            return R.error().message("手机号格式错误！");
        }
        if (address.getReceiverName().length() >= 15 ) {
            return R.error().message("您的名字过长！");
        } else if (address.getReceiverName().equals(" ")) {
            return R.error().message("您的名字不能为空！");
        }
        int i = this.addressService.insert(address);
        if (i > 0) {
            return R.ok().message("添加成功！");
        } else {
            return R.ok().message("添加失败！");
        }
    }

    /**
     * 编辑数据
     *
     * @param address 实体
     * @return 编辑结果
     */
    @PutMapping
    public R edit(@RequestBody Address address) {
        String phoneRegex = "^1[3-9]\\d{9}$";
        if (!address.getReceiverPhone().matches(phoneRegex)) {
            return R.error().message("手机号格式错误！");
        }
        if (address.getReceiverName().length() >= 15 ) {
            return R.error().message("您的名字过长！");
        } else if (address.getReceiverName().equals(" ")) {
            return R.error().message("您的名字不能为空！");
        }
        int i = this.addressService.update(address);
        if (i > 0) {
            return R.ok().message("修改成功！");
        } else {
            return R.ok().message("修改失败！");
        }
    }

    /**
     * 修改默认地址
     *
     * @param id 收货地址id
     * @param request 用户id
     * @return 影响行数
     */
    @PutMapping("/setDefault")
    public R setDefault(@RequestParam Long id, HttpServletRequest request) {
        // 1. 获取当前登录用户ID
        String userIdStr = (String) request.getAttribute("userId");
        if (userIdStr == null) {
            return R.error().message("用户未登录 or 携带token请求");
        }
        Long userId = Long.parseLong(userIdStr);
        int i = this.addressService.setDefault(id, userId);
        if (i > 0) {
            return R.ok().message("修改成功！");
        } else {
            return R.ok().message("修改失败！");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") Long id, HttpServletRequest request) {
        String userIdStr = (String) request.getAttribute("userId");
        Long userId = Long.parseLong(userIdStr);
        int i = this.addressService.deleteById(id, userId);
        if (i > 0) {
            return R.ok().message("删除成功！");
        } else {
            return R.ok().message("删除失败！");
        }
    }

}

